﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using CustomWindows.Window_API;
using System.Threading;
namespace CustomWindows
{

    public partial class FrmCustomWindow : Form
    {
		#region Fields (4) 

        private const int Hotkey = 0x0312; 
        private Hotkeys _hotkeys;
        private IntPtr _windowCustomizerHwnd;
        private readonly WindowsFunctionFactory _factory = new WindowsFunctionFactory();

		#endregion Fields 

		#region Constructors (1) 

        public FrmCustomWindow()
        {
            InitializeComponent();
        }

		#endregion Constructors 

		#region Methods (10) 

		// Protected Methods (1) 

        

        /// <summary>
        /// Determines action associated with hotkey
        /// </summary>
        /// <param name="keyPressed"></param>
        protected override void WndProc(ref Message keyPressed)
        {
            if (keyPressed.Msg == Hotkey) // When user hits the key combination associated with registered hotkeys
            {
                var hwnd = GetForegroundWindow();
                var factory = _factory.CreateFunction(hwnd, keyPressed.WParam);
                factory.UpdateFunction();
                UpdateEventLog(factory.EventLogger);
            }
            base.WndProc(ref keyPressed);
        }

        private void UpdateEventLog(Event eventLogger)
        {
            lstEvents.Items.Add(eventLogger.ToString());
            lstEvents.SelectedIndex = lstEvents.Items.Count - 1;
        }

		// Private Methods (9) 

        private void BtnRefreshClick(object sender, EventArgs e)
        {
            var applications = Process.GetProcesses()
                .Where(x => !String.IsNullOrEmpty(x.MainWindowTitle))
                .OrderBy(x => x.MainWindowTitle);
            lstApp.DataSource = applications.ToList();
            lstApp.DisplayMember = "MainWindowTitle";
        }

        private void FrmCustomWindowFormClosing(object sender, FormClosingEventArgs e)
        {
            _hotkeys.UnregisterHotKeys();
            ntiIcon.Visible = false;
        }

        private void FrmCustomWindowLoad(object sender, EventArgs e)
        {
            // hwnd for this form
            _windowCustomizerHwnd = FindWindow(null, "Windows Customizer");

            // Register hotkeys
            _hotkeys = new Hotkeys(_windowCustomizerHwnd);
            _hotkeys.RegisterHotkeys();

            BtnRefreshClick(null, null);
        }

        private void FrmCustomWindowResize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized != WindowState) return;
            Hide();
            ntiIcon.Visible = true;
            ntiIcon.ShowBalloonTip(1000);
        }

        private void NotifyIcon1MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
            ntiIcon.Visible = false;
        }
        private void LstAppMouseDoubleClick(object sender, MouseEventArgs e)
        {
            richTextBox1.Clear();
            var process = (Process)lstApp.SelectedItem;

            richTextBox1.AppendText("Process Name: " + process.ProcessName + "\n");
            richTextBox1.AppendText("Main Window Title: " + process.MainWindowTitle + "\n");
            richTextBox1.AppendText("Main Window Handle: " + process.MainWindowHandle + "\n");
            richTextBox1.AppendText("Start Time: " + process.StartTime + "\n");
            richTextBox1.AppendText("Responding: " + process.Responding + "\n");
            richTextBox1.AppendText("ID: " + process.Id + "\n");
        }
		#endregion Methods 

        private void button1_Click(object sender, EventArgs e)
        {
            Test.RegisterMouse();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Test.UnRegisterMouse();
        }

        private void btnMouse_Click(object sender, EventArgs e)
        {
            
        }

        private void cmbAlphaOne_SelectedIndexChanged(object sender, EventArgs e)
        {
                    }

        private void lstEvents_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}